
import 'package:flutter/material.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:xim/common/util/collection.dart';
import 'package:xim/common/util/device.dart';


/// 底部弹窗
void bottomSheet(BuildContext context, {
  required Widget child,
  Color bgColor=Colors.white,
  bool overlay=true,
  ModalScrollController? controller,
  BoxDecoration? boxDecoration,
  EdgeInsets? padding,
  bool isScrollControlled = true
}){
  var scrollController = controller?? ModalScrollController.of(context);
  showModalBottomSheet(
    context: context,
    isScrollControlled: isScrollControlled,
    backgroundColor:  Colors.transparent,
    builder: (context) => Container(
      padding: padding,
      decoration: boxDecoration ?? BoxDecoration(
          color: bgColor,
          borderRadius: const BorderRadius.only(
              topLeft: Radius.circular(18),
              topRight: Radius.circular(18)
          )
      ),
      child: child,
    ),
  );
}

/// 底部选择弹出框
selectedBottomSheet<T>(
    BuildContext context,
    Iterable<T> items, {
    required String Function(T item) getItem,
    TextStyle? Function(T item)? itemStyle,
    bool Function(String item)? itemFilter,
    bool showCancelItem = true,
    double? height,
    double verticalPadding =12,
    double gap = 18,
    required void Function(T selectedItem) selectedCallBack,
}){
  var darkMode = PlatformUtils.isDarkMode(context);
  var textStyle = darkMode?const TextStyle(color: Color(0xffd5d5d5)):const TextStyle();
  List<Widget> columns = items
      .map((e) => (getItem(e),e))
      .where((element) => itemFilter!=null?itemFilter(element.$1):true)
      .map((e) => GestureDetector(
      onTap:(){
        selectedCallBack(e.$2);
      },
      child: Center(child: Text(e.$1, style:textStyle,)),
    )).toList();

  columns = columns.splitJoin(SizedBox(height: gap,));

  return bottomSheet(
    context,
    bgColor: darkMode?const Color(0xff2c2c2c):Colors.white,
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.end,
      mainAxisSize: MainAxisSize.min,
      children: [
        ...columns,
        if(showCancelItem)  SizedBox(height: verticalPadding,),
        if(showCancelItem) Column(
          children: [
            Container(height: 8,color: darkMode?const Color(0xff1e1e1e):const Color(0xfff7f8fa),),
            SizedBox(height: verticalPadding,),
            InkWell(
              onTap: ()=> Navigator.pop(context),
              child: Center(
                child: Text("取消", style: textStyle,),
              ),
            ),
          ],
        )
      ],
    ),
    padding: EdgeInsets.symmetric(vertical: verticalPadding)
  );
}
